<?php

namespace  app\index\controller;

use app\common\controller\IndexController;
use app\index\model\LcCardsTran;
use app\index\model\FundCheck;
use app\index\model\UserCashLog;
use app\index\model\Message as messageModel;
use app\index\model\UserLog;
use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\facade\View;

/**
 * 首页管理
 * Class Index
 * @package app\admin\controller
 */
class Home extends  IndexController {

    public function __construct(App $app)
    {
        parent::__construct($app);
        View::assign([
            'menu_class' => 'index',
            'menu_child' => ''
        ]);
    }

    /**
     * 首页
     * @author 救火队队长
     */
    public function  index() {
        try {
            $user_list = Db::name('user')->find($this->user_id);
            $cardDel   = Db::name('cards')->where(['user_id'=>$this->user_id , 'status'=>'3'])->count();
            $cardCount = Db::name('cards')->where(['user_id'=>$this->user_id])->count();
            $cardSetList        = $this->getCardSet();
            $card_max           = $cardSetList['max_card'] ?? 1000; //建卡上限
            $declined_count     = Db::name('cards_trans')
                ->where([
                    'user_id'       =>  $this->user_id,
                    'tran_status'   =>  'Declined'
                ])->count();
            $tran_count     = Db::name('cards_trans')
                ->where([
                    'user_id'       =>  $this->user_id])->count();
            //拒付比率
            $tran_declined_rate = 0;
            if($tran_count != 0){
                $tran_declined_rate    = round($declined_count/$tran_count ,2) * 100;
                if($tran_declined_rate != 0){
                    $tran_declined_rate = getSprintf($tran_declined_rate);
                }
            }

            $declined_amount    = Db::name('cards_trans')
                ->where([
                    'user_id'       =>  $this->user_id,
                    'tran_status'   =>  'Declined'
                ])->sum('amount_auth');
    //        $credit_count     = Db::name('cards_trans_credit')->alias('ctc')
    //            ->join('cards c','c.card_num=ctc.card_num')
    //            ->where([
    //                'c.user_id'         =>  $this->user_id,
    //                'ctc.credit_status' =>  1,
    //            ])->count();
    //        $credit_amount    = Db::name('cards_trans_credit')->alias('ctc')
    //            ->join('cards c','c.card_num=ctc.card_num')
    //            ->where([
    //                'c.user_id'         =>  $this->user_id,
    //                'ctc.credit_status' =>  1,
    //            ])->sum('ctc.amount');
            $credit_count1     = Db::name('cards_trans_credit')
                ->where([
                    'user_id'         =>  $this->user_id,
                    'credit_status'   =>  1,
                ])->count();
            $credit_count2     = Db::name('cards_trans_credit_cd')
                ->where([
                    'user_id'         =>  $this->user_id,
                ])->count();
            $credit_count3     = Db::name('cards_trans_credit_lss')
                ->where([
                    'user_id'         =>  $this->user_id,
                ])->count();
            $credit_count = $credit_count1 + $credit_count2 + $credit_count3;
            $credit_amount1    = Db::name('cards_trans_credit')
                ->where([
                    'user_id'         =>  $this->user_id,
                    'credit_status'   =>  1,
                ])->sum('amount');
            $credit_amount2     = Db::name('cards_trans_credit_cd')
                ->where([
                    'user_id'         =>  $this->user_id,
                ])->sum('amount');
            $credit_amount3     = Db::name('cards_trans_credit_lss')
                ->where([
                    'user_id'         =>  $this->user_id,
                ])->sum('amount');
            $credit_amount = getSprintf($credit_amount1 + $credit_amount2 + $credit_amount3);
            //判断显示规则是否超过72小时
            $is_open   = Db::name('user')->where(['id'=>$this->user_id])->value('is_open');

            $is_notice = 0;
            $current_time = getUtcTime();
            if(!empty($is_open)){
                if($current_time - $is_open >= 24*60*60){
                    $is_notice = 1;
                    Db::name('user')->where(['id'=> $this->user_id])->update(['is_open'=> $current_time]);
                }
            }else{
                $is_notice = 1;
                Db::name('user')->where(['id'=>$this->user_id])
                    ->update(['is_open'=> $current_time]);
            }
            $cardSetList2        = $this->getCardSet(false);
            $show_card      =[];
            $show_1         =[];
            if($cardSetList2){
                foreach ($cardSetList2 as $k_3=>$v_3){
                    $is_used    = $v_3['is_used'];
                    $card_type  = $v_3['card_type'];
                    if($is_used != 1){
                        $show_1['card_type']    = $card_type;
                        $show_1['card_num']     = Db::name('cards')
                            ->where([
                                'user_id'   =>$this->user_id,
                                'card_type' => $card_type
                            ])->count();
                        $show_card[] = $show_1;
                    }
                }
            }
            $total_fund_amount = Db::name('fund_check')->where("check_status = 1 and type=0 and user_id ={$this->user_id}")->sum('amount');
            $total_fund_amount_debit = Db::name('fund_check')->where("check_status = 1 and type=1 and user_id ={$this->user_id}")->sum('amount');
            $total_fund_amount -= $total_fund_amount_debit;
        }catch (\Exception $e){
            //var_dump($e->getMessage() . '---' .$e->getLine());
        }
        View::assign([
            'balance'       => $user_list['balance'],
            'grade'         => $user_list['grade'],
            'card_max'      => $card_max,
            'cardDel'       => $cardDel,
            'cardCount'     => $cardCount,
            'showCards'     => array_slice($show_card , 0 ,5),
            'showCardsCount'    => count($show_card),
            'declined_count'    => $declined_count,
            'declined_amount'   => $declined_amount,
            'credit_count'      => $credit_count,
            'total_fund_amount' => $total_fund_amount,
            'tran_declined_rate'=>$tran_declined_rate,
            'credit_amount'     => abs($credit_amount),
            'is_notice'     => $is_notice
        ]);
        View::layout(true);
        return View::fetch();
    }

    public function getText()
    {
        $id = input('post.id');
        $list = Db::name('notice')->where(['id'=>$id])->value('text');
        return show(1,'',['desc'=>$list]);
    }

    public function regNotice()
    {
        $list  = Db::name('notice_index')->order('id desc')->find();
        View::assign([
            'text'          => $list['text'],
        ]);
        View::layout(false);
        return View::fetch();
    }

    public function moreCard()
    {
        $cardSetList2        = $this->getCardSet(false);
        $show_card      =[];
        $show_1         =[];
        foreach ($cardSetList2 as $k_3=>$v_3){
            $is_used    = $v_3['is_used'];
            $card_type  = $v_3['card_type'];
            if($is_used != 1){
                $show_1['card_type']    = $card_type;
                $show_1['card_num']     = Db::name('cards')
                    ->where([
                        'user_id'   =>$this->user_id,
                        'card_type' => $card_type
                    ])->count();
                $show_card[] = $show_1;
            }
        }
        View::assign([
            'showCards'     => $show_card,
        ]);
        View::layout(false);
        return View::fetch();
    }

    public function logout()
    {
        //增加退出日志
        if(session('user.id')){
            UserLog::setLog(session('user.id'),UserLog::USER_LOGOUT);
        }
        Session('user', null);
        Session('user_token', null);
        session('home_name', null);
        Cache::store('redis')->delete($this->username .  '_login');

        return redirect(url('index/'));
    }
}
